<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
  <title>WebRTC Automated Test</title>
  <script type="text/javascript" src="test_functions.js"></script>
  <script>
  'use strict';
  let stream;
  let waitForImageCapture;

  let microphonePermissionStatus;
  let cameraPermissionStatus;
  let panTiltZoomPermissionStatus;

  let queryPermissions = Promise.all([
    navigator.permissions.query({ name: "microphone" }),
    navigator.permissions.query({ name: "camera" }),
    navigator.permissions.query({ name: "camera", panTiltZoom: true }),
  ]).then((permissionStatuses) => {
    [
      microphonePermissionStatus,
      cameraPermissionStatus,
      panTiltZoomPermissionStatus,
    ] = permissionStatuses;
  });

  async function runGetUserMedia(constraints) {
    try {
      await queryPermissions;
      stream = await navigator.mediaDevices.getUserMedia(constraints);
      waitForImageCapture = new Promise(resolve => setTimeout(resolve, 100));
      returnToTest("runGetUserMedia-success");
    } catch (error) {
      returnToTest("runGetUserMedia-failure");
    }
  }

  async function runRequestPanTiltZoom() {
    try {
      await queryPermissions;
      await navigator.permissions.request({ name: "camera", panTiltZoom: true });
      returnToTest("runRequestPanTiltZoom-success");
    } catch (error) {
      returnToTest("runRequestPanTiltZoom-failure");
    }
  }

  function getTrackSetting(settingName) {
    const videoTrack = stream.getVideoTracks()[0];
    const settings = videoTrack.getSettings();
    returnToTest(settings[settingName]);
  }

  function checkConstraints(expectedConstraints) {
    const videoTrack = stream.getVideoTracks()[0];
    const constraints = videoTrack.getConstraints();

    const s = (o) => Object.entries(o).sort().map(i => {
      if(i[1] instanceof Object) i[1] = s(i[1]);
      return i
    })
    const constraintsString = JSON.stringify(s(constraints));
    const expectedConstraintsString = JSON.stringify(s(expectedConstraints));
    if  (constraintsString !== expectedConstraintsString) {
      debug(`Expected ${expectedConstraintsString} - got ${constraintsString}`)
      returnToTest("checkConstraints-failure");
    }

    returnToTest("checkConstraints-success");
  }

  async function applyConstraints(constraints) {
    try {
      const videoTrack = stream.getVideoTracks()[0];
      await videoTrack.applyConstraints(constraints);
      returnToTest("applyConstraints-success");
    } catch (error) {
      returnToTest(`applyConstraints-failure-${error.name}`);
    }
  }

  function getMicrophonePermission() {
    returnToTest(microphonePermissionStatus.state);
  }

  function getCameraPermission() {
    returnToTest(cameraPermissionStatus.state);
  }

  function getPanTiltZoomPermission() {
    returnToTest(panTiltZoomPermissionStatus.state);
  }

  document.addEventListener('visibilitychange', () => {
    document.title = document.visibilityState;
  });
  </script>
</head>
<body>
</body>
</html>

